package com.bdqn.wang.dao.impl;

import com.bdqn.wang.dao.ComdityAddDao;
import com.bdqn.wang.dao.ComdityAdmDao;
import com.bdqn.wang.pojo.Comdity;
import com.bdqn.wang.pojo.ComdityAdd;
import com.bdqn.wang.pojo.ComdityAdm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ComdityAddDaoImpl extends BaseDao implements ComdityAddDao {


    @Override
    public List<ComdityAdd> getAll() {

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            String sql = "SELECT * FROM easybuy_product_category where parentId=0";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            List<ComdityAdd> comdityAdds = new ArrayList<ComdityAdd>();
            while (resultSet.next()){
                ComdityAdd comdityAdd = new ComdityAdd();
                comdityAdd.setId(resultSet.getInt(1));
                comdityAdd.setName(resultSet.getString(2));
                comdityAdd.setType(resultSet.getInt(3));
                comdityAdd.setParentId(resultSet.getInt(4));
                comdityAdds.add(comdityAdd);
            }
            return comdityAdds;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(resultSet,preparedStatement,connection);
        }
        return null;
    }

    @Override
    public List<ComdityAdd> getAll(int tow) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            String sql = "SELECT\n" +
                    "easybuy_product_category.id,\n" +
                    "easybuy_product_category.NAME FROM easybuy_product_category\n" +
                    "WHERE parentId=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1,tow);
            resultSet = preparedStatement.executeQuery();
            List<ComdityAdd> comdityAdds = new ArrayList<ComdityAdd>();
            while (resultSet.next()){
                ComdityAdd comdityAdd = new ComdityAdd();
                comdityAdd.setId(resultSet.getInt(1));
                comdityAdd.setName(resultSet.getString(2));
                comdityAdds.add(comdityAdd);
            }
            return comdityAdds;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(resultSet,preparedStatement,connection);
        }
        return null;
    }

    @Override
    public List<ComdityAdd> getAllThree(int three) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            String sql = "SELECT easybuy_product_category.id," +
                    " easybuy_product_category.NAME FROM easybuy_product_category\n" +
                    "WHERE parentId=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1,three);
            resultSet = preparedStatement.executeQuery();
            List<ComdityAdd> comdityAdds = new ArrayList<ComdityAdd>();
            while (resultSet.next()){
                ComdityAdd comdityAdd = new ComdityAdd();
                comdityAdd.setId(resultSet.getInt(1));
                comdityAdd.setName(resultSet.getString(2));
                comdityAdds.add(comdityAdd);
            }
            return comdityAdds;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(resultSet,preparedStatement,connection);
        }
        return null;
    }

}
